﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SLRTHackathon.DataModels;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;

namespace SLRTHackathon.ViewModels
{
    public class SLRTViewModel: INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private SLRTDataContext SLRTDb;

        // Class constructor, create the data context object.
        public SLRTViewModel(string SLRTDBConnectionString)
        {
            SLRTDb = new SLRTDataContext(SLRTDBConnectionString);
        }

        // All cars.
        private ObservableCollection<Car> _allCars;
        public ObservableCollection<Car> AllCars
        {
            get { return _allCars; }
            set
            {
                _allCars = value;
                NotifyPropertyChanged("AllCars");
            }
        }

        // All insurances.
        private ObservableCollection<Insurance> _allInsurances;
        public ObservableCollection<Insurance> AllInsurances
        {
            get { return _allInsurances; }
            set
            {
                _allInsurances = value;
                NotifyPropertyChanged("AllInsurances");
            }
        }

        // All extended insurances.
        private ObservableCollection<ExtendedInsurance> _allExtendedInsurances;
        public ObservableCollection<ExtendedInsurance> AllExtendedInsurances
        {
            get { return _allExtendedInsurances; }
            set
            {
                _allExtendedInsurances = value;
                NotifyPropertyChanged("AllExtendedInsurances");
            }
        }

        // All customized dependencies.
        private ObservableCollection<CustomDependency> _allCustomDependencies;
        public ObservableCollection<CustomDependency> AllCustomDependencies
        {
            get { return _allCustomDependencies; }
            set
            {
                _allCustomDependencies = value;
                NotifyPropertyChanged("AllCustomDependencies");
            }
        }

        // All international vignettes.
        private ObservableCollection<InternationalVignette> _allInternationalVignettes;
        public ObservableCollection<InternationalVignette> AllInternationalVignettes
        {
            get { return _allInternationalVignettes; }
            set
            {
                _allInternationalVignettes = value;
                NotifyPropertyChanged("AllInternationalVignettes");
            }
        }

        // A list of all insurances for a certain car.
        private ObservableCollection<Insurance> _insuranceForCar;
        public ObservableCollection<Insurance> InsuranceForCar
        {
            get { return _insuranceForCar; }
            set
            {
                _insuranceForCar = value;
                NotifyPropertyChanged("InsuranceForCar");
            }
        }

        // A list of all extendedInsurances for a certain car.
        private ObservableCollection<ExtendedInsurance> _extendedInsuranceForCar;
        public ObservableCollection<ExtendedInsurance> ExtendedInsuranceForCar
        {
            get { return _extendedInsuranceForCar; }
            set
            {
                _extendedInsuranceForCar = value;
                NotifyPropertyChanged("ExtendedInsuranceForCar");
            }
        }

        // A list of all customDependencies for a certain car.
        private ObservableCollection<CustomDependency> _customDependencyForCar;
        public ObservableCollection<CustomDependency> CustomDependencyForCar
        {
            get { return _customDependencyForCar; }
            set
            {
                _customDependencyForCar = value;
                NotifyPropertyChanged("CustomDependencyForCar");
            }
        }

        // A list of all internationalVignettes for a certain car.
        private ObservableCollection<InternationalVignette> _internationalVignetteForCar;
        public ObservableCollection<InternationalVignette> InternationalVignetteForCar
        {
            get { return _internationalVignetteForCar; }
            set
            {
                _internationalVignetteForCar = value;
                NotifyPropertyChanged("InternationalVignetteForCar");
            }
        }

        // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {

            // Specify the query for all cars in the database.
            var carsInDB = from Car car in SLRTDb.Cars select car;

            // Query the database and load all cars.
            AllCars = new ObservableCollection<Car>(carsInDB);

            // Specify the query for all insurances in the database.
            var insuranceInDb = from Insurance insurance in SLRTDb.Insurances select insurance;

            // Query the database and load all insurances.
            AllInsurances = new ObservableCollection<Insurance>(insuranceInDb);

            // Specify the query for all extended insurances in the database.
            var extendedInsuranceInDb = from ExtendedInsurance extendedInsurance in SLRTDb.ExtendedInsurances select extendedInsurance;

            // Query the database and load all extended insurances.
            AllExtendedInsurances = new ObservableCollection<ExtendedInsurance>(extendedInsuranceInDb);

            // Specify the query for all custom dependecies in the database.
            var customDependencies = from CustomDependency customDepencies in SLRTDb.CustomDependencies select customDepencies;

            // Query the database and load all custom dependecies.
            AllCustomDependencies = new ObservableCollection<CustomDependency>(customDependencies);

            // Specify the query for all international vignettes in the database.
            var internationalVignette = from InternationalVignette vignette in SLRTDb.InternationalVignettes select vignette;

            // Query the database and load all international vignettes.
            AllInternationalVignettes = new ObservableCollection<InternationalVignette>(internationalVignette);
        }

        public void LoadDependenciesForCar(Car currentCar)
        {
            // Specify the query for all insurances for a specific car in the database.
            var insurancesForCarInDB = from Insurance insurance in SLRTDb.Insurances
                                       where insurance._carId == currentCar.CarId
                                       select insurance;

            // Query the database and load all insurences for a specific car.
            InsuranceForCar = new ObservableCollection<Insurance>(insurancesForCarInDB); 

            // Specify the query for all extended insurances for a specific car in the database.
            var extendInsurenceForCar = from ExtendedInsurance extendedInsurance in SLRTDb.ExtendedInsurances
                                  where extendedInsurance._carId == currentCar.CarId
                                  select extendedInsurance;

            // Query the database and load all extended insurences for a specific car.
            ExtendedInsuranceForCar = new ObservableCollection<ExtendedInsurance>(extendInsurenceForCar);

            // Specify the query for all custom dependencies for a specific car in the database.
            var customDependencyForCar = from CustomDependency customDependency in SLRTDb.CustomDependencies
                                  where customDependency._carId == currentCar.CarId
                                  select customDependency;

            // Query the database and load all custom dependencies for a specific car.
            CustomDependencyForCar = new ObservableCollection<CustomDependency>(customDependencyForCar);

            // Specify the query for all international vignettes for a specific car in the database.
            var internationalVignetteForCar = from InternationalVignette internationalVignette in SLRTDb.InternationalVignettes
                                  where internationalVignette._carId == currentCar.CarId
                                  select internationalVignette;

            // Query the database and load all international vignettes for a specific car.
            InternationalVignetteForCar = new ObservableCollection<InternationalVignette>(internationalVignetteForCar);
        }

        // Add a car to the database and collections.
        public void AddCar(Car newCar)
        {
            // Add a car to the data context.
            SLRTDb.Cars.InsertOnSubmit(newCar);

            // Save changes to the database.
            SLRTDb.SubmitChanges();

            // Add a car to the "all" observable collection.
            AllCars.Add(newCar);
        }

        public void AddInsurence(Insurance newInsurence)
        {
            SLRTDb.Insurances.InsertOnSubmit(newInsurence);
            SLRTDb.SubmitChanges();
            InsuranceForCar.Add(newInsurence);
        }

        public void AddExtendedInsurence(ExtendedInsurance newExtendedInsurence)
        {
            SLRTDb.ExtendedInsurances.InsertOnSubmit(newExtendedInsurence);
            SLRTDb.SubmitChanges();
            ExtendedInsuranceForCar.Add(newExtendedInsurence);
        }

        public void AddCustomDependency(CustomDependency newCustomDependency)
        {
            SLRTDb.CustomDependencies.InsertOnSubmit(newCustomDependency);
            SLRTDb.SubmitChanges();
            CustomDependencyForCar.Add(newCustomDependency);
        }

        public void AddInternationalVignette(InternationalVignette newIntVignette)
        {
            SLRTDb.InternationalVignettes.InsertOnSubmit(newIntVignette);
            SLRTDb.SubmitChanges();
            InternationalVignetteForCar.Add(newIntVignette);
        }

        // Remove a car from the database and collections.
        public void DeleteCar(Car carToBeDeleted)
        {

            // Remove the to-do item from the "all" observable collection.
            AllCars.Remove(carToBeDeleted);

            // Remove the to-do item from the data context.
            SLRTDb.Cars.DeleteOnSubmit(carToBeDeleted);

            // Save changes to the database.
            SLRTDb.SubmitChanges();
        }

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            SLRTDb.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
